package com.telkomsigma.hibernate.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.telkomsigma.hibernate.persistence.team.entity.Team;
import com.telkomsigma.hibernate.services.team.TeamService;

@Controller
@SessionAttributes
@Transactional
public class ControllerTeam{
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	TeamService teamService = (TeamService)context.getBean("teamService");
	
	@RequestMapping(value = "/team", method = RequestMethod.GET)
    public ModelAndView listOfTeam() {
		List<Team> teamList = teamService.findAll(0,5);
		Map<String, Object> teamMap = new HashMap<String, Object>();
		teamMap.put("teamListMap", teamList);
		teamMap.put("moduleTeamMap","list");
        return new ModelAndView("team","teamMapAttribute", teamMap);
    }
	
	
	@RequestMapping(value = "/teamForm", method = RequestMethod.GET)
	public ModelAndView setupForm(@RequestParam("module") String module){
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("moduleTeamMap", "add");
		return new ModelAndView("team","teamFormAttribute", formMap);		
	}
	
	@RequestMapping(value = "/team", method = RequestMethod.POST)
	public ModelAndView saveTeam(@ModelAttribute("team") Team team,@RequestParam("teamName") String teamName){		
		team.setName(teamName);
		team.getName();
		teamService.insert(team);
		Map<String, Object> teamQuery = new HashMap<String, Object>();		
		teamQuery.put("message", "Insert Success");
		return new ModelAndView("team","teamQuery", teamQuery);
	}
	
	@RequestMapping(value = "/teamEdit", method = RequestMethod.GET)
	public ModelAndView editTeam(@RequestParam("teamId") String teamId){		
		Team teamFindId = teamService.findById(teamId);
		Map<String, Object> teamEdit = new HashMap<String, Object>();
		teamEdit.put("teamId", teamFindId.getId());
		teamEdit.put("teamName", teamFindId.getName());
		teamEdit.put("moduleTeamMap", "edit");
		return new ModelAndView("team","teamFormAttribute",teamEdit);
	}
	
	@RequestMapping(value = "/teamEdit", method = RequestMethod.POST)
	public ModelAndView editSaveTeam(@RequestParam("teamId") String teamId, @RequestParam("teamName") String teamName){
		Team team = new Team();
		team.setId(teamId);
		team.setName(teamName);
		team.getId();
		team.getName();		
		teamService.update(team);
		Map<String, Object> teamQuery = new HashMap<String, Object>();		
		teamQuery.put("message", "Update Success");			

		return new ModelAndView("team","teamQuery", teamQuery);		
	}
	
	@RequestMapping(value = "/teamDelete", method = RequestMethod.GET)
	public ModelAndView deleteTeam(@RequestParam("teamId") String teamId){
		Team team = new Team();
		team.setId(teamId);
		teamService.remove(team);
		Map<String, Object> teamQuery = new HashMap<String, Object>();
		teamQuery.put("message", "Delete Success");			
		return new ModelAndView("team","teamQuery", teamQuery);		
	}
}
